﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Robotics
{
	class SequentialActivityManager
	{
		private LinkedList<Activity> activities = new LinkedList<Activity>();
		private Activity currentActivity = null;

		public bool Tick()
		{
			if ( currentActivity == null && activities.Count != 0 )
			{
				currentActivity = activities.First.Value;
				activities.RemoveFirst();
				currentActivity.Start();
			}
			while ( currentActivity != null && !currentActivity.Tick() )
			{
				currentActivity.End();
				currentActivity = null;
				if ( activities.Count != 0 )
				{
					currentActivity = activities.First.Value;
					activities.RemoveFirst();
					currentActivity.Start();
				}
			}
			return true;
		}

		public void Add( Activity activity )
		{
			activities.AddLast( activity );
		}
	}
}
